// 实现一个JSON
const json = {
  parse(obj) {
    return eval("(" + obj + ")");
  },
  stringify: (function () {
    const toString = Object.prototype.toString;

    let isArray =
      Array.isArray ||
      function (a) {
        return toString.call(a) === "[object Array]";
      };

    return function stringify(value) {
      if (value == null) {
        return "null";
      } else if (typeof value === "number") {
        return isFinite(value) ? value.toString() : "null";
      } else if (typeof value === "boolean") {
        return value.toString();
      } else if (typeof value === "function") {
        return "null";
      } else if (typeof value === "object") {
        if (isArray(value)) {
          let res = "[";
          for (let i = 0; i < value.length; i++) {
            res += (i > 0 ? "," : "") + stringify(value[i]);
          }
          return res + "]";
        } else if (toString.call(value) === "[object Object]") {
          const tmp = [];
          for (let k in value) {
            if (value.hasOwnProperty(k)) {
              tmp.push(stringify(k) + ":" + stringify(value[k]));
            }
          }
          return "{" + tmp.join(",") + "}";
        }
      }
      return '"' + value.toString() + '"';
    };
  })(),
};

const b = {
  app: function () {},
  x: NaN,
  y: "test",
  z: undefined,
  a: [1, 2, 3],
  b: { a: 2, c: { key: 123 } },
};

console.log(JSON.stringify(b));

console.log(json.parse(json.stringify(b)));

// isFinite   测试数值是否是有限数字
